$(function() {
    "use strict";

    $('#dashboard-table').dataTable({
        "sPaginationType": "bootstrap",
        "bFilter": true,
        "bLengthChange": false,
        "iDisplayLength": 15,
        "bAutoWidth": false,
        "sDom": '<"top"ft><"bottom"p><"clear">',
        "aoColumns": [
            { "sWidth": "25%", "sClass": "center", "sType": "html" },
            { "sWidth": "25%", "sClass": "center" },
            { "sWidth": "25%", "sClass": "center" },
            { "sWidth": "25%", "sClass": "center", "bSortable": false }
        ]
    });

    //// DELETE CLIENT ////

    $('#dashboard-delete-client-modal').on('submit', '#dashboard-delete-client-form', function() {
        var $form = $(this);

        $.post($form.attr('action'), $form.serialize())
            .done(function(data, status) {
                var contactId = $form.find('input[name=contactId]').val();
                var $dataTable = $('#dashboard-table').dataTable();

                $dataTable.fnDeleteRow($('#dashboard-table-row-' + contactId).get(0));
            })
            .fail(function() {
                addGlobalErrorAlert();
            })
            .always(function() {
                $('#dashboard-delete-client-modal').modal('hide');
            });

        return false;
    });

    $('#dashboard-table').on('click', '.client-delete-button', function() {
        var $deleteButton = $(this);

        $('#dashboard-delete-client-form .modal-body p')
            .html("Are you sure you want to delete this client: " + $deleteButton.attr('displayName') + "?");

        $('#dashboard-delete-client-form .modal-body input[name=contactId]')
            .val($deleteButton.attr('contactId'));
    });


    //// EDIT CLIENT /////

    $('#dashboard-edit-client-modal').on('submit', '#dashboard-edit-client-form', function() {
        var $form = $(this);

        $.post($form.attr('action'), $form.serialize())
            .done(function(data, status) {
                console.log("Post completed to: " + $form.serialize())

            })
            .fail(function() {
                addGlobalErrorAlert();
            })
            .always(function() {
                $('#dashboard-edit-client-modal').modal('hide');
            });

        return false;
    });


//    $('#describe-client-save-button').on('click', function() {
//        saveModalFields();
//        return false;
//    });


    ///// ADD CLIENT ////
    $('#dashboard-add-client-modal').on('submit', '#dashboard-add-client-form', function() {
        var $form = $(this);

        $.post($form.attr('action'), $form.serialize())
            .done(function(data, status) {
                console.log("Post completed to: " + $form.serialize())
                location.reload(true);
            })
            .fail(function() {
                addGlobalErrorAlert();
            })
            .always(function() {
                $('#dashboard-add-client-modal').modal('hide');
            });

        return false;
    });


});